﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Public Class DBManager
    Private Property cnx As SqlConnection

    Public Sub New()
        If cnx Is Nothing Then
            cnx = New SqlConnection()
            cnx.ConnectionString = ConfigurationManager.ConnectionStrings("ApplicationServices").ConnectionString
        End If
    End Sub

    Public Function CreateSelectCommand(ByVal comando As String) As SqlCommand
        Dim command As New SqlCommand
        command.CommandText = comando
        command.CommandType = CommandType.StoredProcedure
        command.Connection = cnx
        Return command
    End Function

    Public Function ExecuteReader(ByVal command As SqlCommand)
        cnx.Open()
        Dim reader As SqlDataReader = command.ExecuteReader(CommandBehavior.CloseConnection)
        Return reader
    End Function

    Public Function ExecuteQuery(ByRef command As SqlCommand)
        cnx.Open()
        Dim query As Integer = command.ExecuteNonQuery()
        cnx.Close()
        Return query
    End Function

End Class